package business.sale.entities;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

import client.entities.ClientVO;
import client.exceptions.ClientNotHereException;
import employee.entities.EmployeeVO;
import sale.entities.OrderVO;
import sale.entities.SaleVO;
import business.BusinessFacade;
import business.article.entities.Article;
import business.client.entities.Client;
import business.employee.entities.Employee;

public class Sale {

	private int saleKey, tableKey;
	private Employee emp;
	private Client client;
	private Date openDate, closeDate;
	private ArrayList<Order> orders;
	private String address;
	private String tel;
	private boolean isOpen;
	private double total;
	


	public int getSaleKey() {
		return saleKey;
	}

	public void setSaleKey(int saleKey) {
		this.saleKey = saleKey;
	}

	public int getTableKey() {
		return tableKey;
	}

	public void setTableKey(int tableKey) {
		this.tableKey = tableKey;
	}

	public Employee getEmp() {
		return emp;
	}

	public void setEmp(Employee emp) {
		this.emp = emp;
	}

	public Client getClient() {
		return client;
	}

	public void setClient(Client client) {
		this.client = client;
	}

	public Date getOpenDate() {
		return openDate;
	}

	public void setOpenDate(Date openDate) {
		this.openDate = openDate;
	}

	public Date getCloseDate() {
		return closeDate;
	}

	public void setCloseDate(Date closeDate) {
		this.closeDate = closeDate;
	}

	public ArrayList<Order> getOrders() {
		return orders;
	}

	public void setOrders(ArrayList<Order> orders) {
		this.orders = orders;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public boolean isOpen() {
		return isOpen;
	}

	public void setOpen(boolean isOpen) {
		this.isOpen = isOpen;
	}

	public double getTotal() {
		return total;
	}

	public void setTotal(double total) {
		this.total = total;
	}

	public String getTel() {
		return tel;
	}

	public void setTel(String tel) {
		this.tel = tel;
	}

	public Sale(OrderVO ord, int tableKey, int ci) {
		//es el constructor para la delivery o take out
		this.tableKey = tableKey;
		this.openDate = Calendar.getInstance().getTime();
		this.orders = new ArrayList<Order>();
		this.orders.add(new Order(ord));
		this.isOpen = true;
		this.address = ord.getAddress();
		this.tel = ord.getTel();
		this.total = this.getTotals(this.orders);
		try {
			this.client = new Client(BusinessFacade.getInstance().getClientMgt().getClient(ci));
		} catch (ClientNotHereException e) {
			this.client = null;
		}
	}

	public Sale(SaleVO sale) {
		if (sale != null) {
			this.tableKey = sale.getTableKey();
			this.openDate = sale.getOpenDate();
			this.isOpen = sale.isOpen();
			this.address = sale.getAddress();
			this.tel = sale.getTel();
			this.orders = new ArrayList<Order>();
			for (OrderVO o : sale.getOrders())
				this.orders.add(new Order(o));
			this.total = (sale.getTotal()==0) ? this.getTotals(this.getOrders()) : sale.getTotal();
			this.total = sale.getTotal();
			this.saleKey = sale.getSaleKey();
			this.emp = new Employee(sale.getEmp());
			this.client = new Client(sale.getClient());
			this.closeDate = sale.getCloseDate();
			
		}
	}

	public Sale(int saleKey, int tableKey, Employee emp, Client client,
			Date openDate, Date closeDate, ArrayList<Order> orders,
			String address, String tel, boolean isOpen) {
		super();
		this.saleKey = saleKey;
		this.tableKey = tableKey;
		this.emp = emp;
		this.client = client;
		this.openDate = openDate;
		this.closeDate = closeDate;
		this.orders = orders;
		this.address = address;
		this.tel = tel;
		this.isOpen = isOpen;
		this.total = this.getTotals(orders);
	}

	public Sale(int saleKey, int tableKey, Employee emp, Client client,
			Date openDate, Date closeDate, ArrayList<Order> orders,
			String address, String tel, boolean isOpen, double total) {
		super();
		this.saleKey = saleKey;
		this.tableKey = tableKey;
		this.emp = emp;
		this.client = client;
		this.openDate = openDate;
		this.closeDate = closeDate;
		this.orders = orders;
		this.address = address;
		this.tel = tel;
		this.isOpen = isOpen;
		this.total = total;
	}

	public SaleVO toVO() {
		ArrayList<OrderVO> temp = new ArrayList<OrderVO>();
		for (Order o : this.orders)
			temp.add(o.toVO());
		ClientVO aux1 = (this.client != null) ? this.client.toVO() : null;
		EmployeeVO aux2 = (this.emp != null) ? this.emp.toVO() : null;
		return new SaleVO(this.saleKey, this.tableKey, aux2, aux1,
				this.openDate, this.closeDate, temp, this.address, this.tel,
				this.isOpen, this.total);
	}

	private double getTotals(Order ord) {
		BigDecimal total = new BigDecimal(0);
		if (ord != null && ord.getArts() != null) {
			for (Article e : ord.getArts()) {
				total = total.add(new BigDecimal(e.getPrice()));
			}
		}
		return total.doubleValue();
	}
	
	private double getTotals(ArrayList<Order> orders){
		BigDecimal total = new BigDecimal(0);
		for(Order o : orders)
			total = total.add(new BigDecimal(this.getTotals(o)));
		return total.doubleValue();
	}

	public void addOrder(OrderVO ord) {
		if (ord != null && ord.getArts() != null) {
			this.orders.add(new Order(ord));
			BigDecimal aux = new BigDecimal(this.total);
			aux = aux.add(new BigDecimal(this.getTotals(new Order(ord))));
			this.total = aux.doubleValue();
		}
	}

	public ArrayList<ArticleBill> prepareToClose(int billId) {
		ArrayList<ArticleBill> temp = new ArrayList<ArticleBill>();
		for (Order o : this.orders) {
			if (o != null) {
				for (Article a : o.getArts()) {
					if (a != null) {
						boolean aux = false;
						for (ArticleBill ab : temp) {
							if (a.compareTo(ab.getArt()) == 0) {
								ab.updateOneUp();
								aux = true;
							}
						}
						if (aux == false)
							temp.add(new ArticleBill(a, billId, temp.size()));
					}
				}
			}

		}
		return temp;
	}


}
